//
//  RunloopViewController.m
//  ReviewDemo
//
//  Created by 龙 on 2018/3/26.
//  Copyright © 2018年 long. All rights reserved.
//

#import "RunloopViewController.h"

#import <Foundation/Foundation.h>
#import <CoreFoundation/CFRunLoop.h>
#import <pthread.h>

@interface RunloopViewController ()
@property (nonatomic ,strong) UIScrollView *scrollView;
@end

@implementation RunloopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    NSLog(@"currentRunLoop ======= %@",[NSRunLoop currentRunLoop]);
    
    [self.view addSubview:self.scrollView];
    [self performSelectorTest];
    
    pthread_main_np();//主线程  [NSThread mainThread]
    pthread_self();//当前线程 [NSThread currentThread]
    
    CFRunLoopGetMain();// [NSRunLoop mainRunLoop]
    CFRunLoopGetCurrent();//[NSRunLoop currentRunLoop]
    
    
    
//    [self creatRunloopObserver];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)performSelectorTest
{
    NSLog(@"%s --------- %@ ========== %@",__func__,[NSThread currentThread],[NSRunLoop currentRunLoop]);

//    [[NSRunLoop currentRunLoop] addTimer:(nonnull NSTimer *) forMode:NSRunLoopCommonModes];
//    [[NSRunLoop currentRunLoop] addPort:(nonnull NSPort *) forMode:(nonnull NSRunLoopMode)];
    
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadSelectorTest) object:nil];
    [thread start];
}

- (void)threadSelectorTest
{
    NSLog(@"%s --------- %@ ========== %@",__func__,[NSThread currentThread],[NSRunLoop currentRunLoop]);
    
    /*
       当调用 NSObject 的 performSelecter:afterDelay: 后，
       实际上其内部会创建一个 Timer 并添加到当前线程的 RunLoop 中。
       所以如果当前线程没有 RunLoop，则这个方法会失效
     */
    [self performSelector:@selector(afterSelectorTest) withObject:nil afterDelay:0.5];
}

// 不执行？ 为什么？
- (void)afterSelectorTest
{
    NSLog(@"%s --------- %@ ========== %@",__func__,[NSThread currentThread],[NSRunLoop currentRunLoop]);
}

- (void)creatRunloopObserver
{
    // 创建observer
    CFRunLoopObserverRef observer =CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity)
    {
        switch (activity) {
            case kCFRunLoopEntry:
                NSLog(@"监听到即将进入RunLoop------%zd----%@",activity,[[NSRunLoop currentRunLoop] currentMode]);
                break;
                
            case kCFRunLoopBeforeTimers://模拟器的时间发生变化时就会触发kCFRunLoopBeforeTimers回调（每分钟
                NSLog(@"监听到即将处理Timer------%zd----%@",activity,[[NSRunLoop currentRunLoop] currentMode]);
                break;
                
            case kCFRunLoopBeforeSources:
                NSLog(@"监听到即将处理Source------%zd----%@",activity,[[NSRunLoop currentRunLoop] currentMode]);
                break;
                
            case kCFRunLoopBeforeWaiting:
                NSLog(@"监听到即将进入睡眠------%zd----%@",activity,[[NSRunLoop currentRunLoop] currentMode]);
                break;
                
            case kCFRunLoopAfterWaiting:
                NSLog(@"监听到即将从睡眠中醒来------%zd----%@",activity,[[NSRunLoop currentRunLoop] currentMode]);
                break;
                
                
            case kCFRunLoopExit:
                NSLog(@"监听到即将从退出RunLoop------%zd----%@",activity,[[NSRunLoop currentRunLoop] currentMode]);
                break;
                
            default:
                break;
        }
        
    });
    
    
    /*
     * CFRunLoopAddObserver函数有三个参数：
     * 第一个：传入一个RunLoop，CFRunLoopGetCurrent()获取当前的RunLoop
     * 第二个：传入一个观察者，observer就是新创建的观察者
     * 第三个：传入Mode，kCFRunLoopCommonModes指定要监听的Mode
     */
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
    CFRelease(observer);
    
}

#pragma mark - setter and getters

//scrollView 滑动时  currentMode 转为 UITrackingRunLoopMode
- (UIScrollView *)scrollView
{
    if (_scrollView == nil)
    {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth)];
        scrollView.backgroundColor = [UIColor redColor];
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 2.0, ScreenHeight)];
        lable.backgroundColor = [UIColor yellowColor];
        [scrollView addSubview:lable];
        scrollView.contentSize = CGSizeMake(ScreenWidth, CGRectGetMaxY(lable.frame));
        
        _scrollView = scrollView;
    }
    return _scrollView;
}

@end
